using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp;
using TenantManagementService.Tenants;

namespace TenantManagementService.Tenants
{
    /// <summary>
    /// 租户管理应用服务接口
    /// </summary>
    public interface ITenantAppService : ICrudAppService<
        TenantDto,
        Guid,
        PagedAndSortedResultRequestDto,
        CreateTenantDto,
        UpdateTenantDto>
    {
        /// <summary>
        /// 根据编码获取租户
        /// </summary>
        Task<TenantDto> GetByCodeAsync(string code);

        /// <summary>
        /// 激活租户
        /// </summary>
        Task<TenantDto> ActivateAsync(Guid id);

        /// <summary>
        /// 暂停租户
        /// </summary>
        Task<TenantDto> SuspendAsync(Guid id, string? reason = null);

        /// <summary>
        /// 禁用租户
        /// </summary>
        Task<TenantDto> DisableAsync(Guid id, string? reason = null);

        /// <summary>
        /// 启用租户
        /// </summary>
        Task<TenantDto> EnableAsync(Guid id);

        /// <summary>
        /// 续期租户
        /// </summary>
        Task<TenantDto> ExtendSubscriptionAsync(Guid id, ExtendSubscriptionDto input);

        /// <summary>
        /// 设置试用
        /// </summary>
        Task<TenantDto> SetTrialAsync(Guid id, SetTrialDto input);

        /// <summary>
        /// 设置管理员信息
        /// </summary>
        Task<TenantDto> SetAdminInfoAsync(Guid id, SetAdminInfoDto input);

        /// <summary>
        /// 设置连接信息
        /// </summary>
        Task<TenantDto> SetConnectionInfoAsync(Guid id, SetConnectionInfoDto input);

        /// <summary>
        /// 更新租户设置
        /// </summary>
        Task<TenantDto> UpdateSettingsAsync(Guid id, UpdateTenantSettingsDto input);

        /// <summary>
        /// 更新功能开关
        /// </summary>
        Task<TenantDto> UpdateFeatureSwitchesAsync(Guid id, UpdateFeatureSwitchesDto input);

        /// <summary>
        /// 更新主题设置
        /// </summary>
        Task<TenantDto> UpdateThemeSettingsAsync(Guid id, UpdateThemeSettingsDto input);

        /// <summary>
        /// 获取租户统计信息
        /// </summary>
        Task<TenantStatisticsDto> GetStatisticsAsync();

        /// <summary>
        /// 获取租户使用率统计
        /// </summary>
        Task<TenantUsageStatisticsDto> GetUsageStatisticsAsync(Guid id);

        /// <summary>
        /// 检查编码是否可用
        /// </summary>
        Task<bool> IsCodeAvailableAsync(string code, Guid? excludeId = null);

        /// <summary>
        /// 检查主机是否可用
        /// </summary>
        Task<bool> IsHostAvailableAsync(string host, Guid? excludeId = null);

        /// <summary>
        /// 获取需要续期的租户
        /// </summary>
        Task<PagedResultDto<TenantDto>> GetTenantsNeedingRenewalAsync(GetTenantsNeedingRenewalDto input);

        /// <summary>
        /// 获取过期的租户
        /// </summary>
        Task<PagedResultDto<TenantDto>> GetExpiredTenantsAsync(GetExpiredTenantsDto input);

        /// <summary>
        /// 获取试用租户
        /// </summary>
        Task<PagedResultDto<TenantDto>> GetTrialTenantsAsync(GetTrialTenantsDto input);

        /// <summary>
        /// 获取超限租户
        /// </summary>
        Task<PagedResultDto<TenantDto>> GetTenantsWithLimitExceededAsync(GetTenantsWithLimitExceededDto input);

        /// <summary>
        /// 搜索租户
        /// </summary>
        Task<PagedResultDto<TenantDto>> SearchAsync(SearchTenantsDto input);

        /// <summary>
        /// 批量操作租户
        /// </summary>
        Task<BatchOperationResultDto> BatchOperateAsync(BatchOperationTenantDto input);

        /// <summary>
        /// 导出租户列表
        /// </summary>
        Task<FileDto> ExportAsync(ExportTenantsDto input);

        /// <summary>
        /// 导入租户列表
        /// </summary>
        Task<ImportResultDto> ImportAsync(ImportTenantsDto input);

        /// <summary>
        /// 获取租户代码列表
        /// </summary>
        Task<List<string>> GetCodesAsync(bool? isEnabled = null);

        /// <summary>
        /// 重置租户密码
        /// </summary>
        Task ResetAdminPasswordAsync(Guid id, ResetAdminPasswordDto input);

        /// <summary>
        /// 验证租户
        /// </summary>
        Task<ValidationResultDto> ValidateAsync(Guid id);

        /// <summary>
        /// 获取租户配置
        /// </summary>
        Task<TenantConfigurationsDto> GetConfigurationsAsync(Guid id);

        /// <summary>
        /// 更新租户配置
        /// </summary>
        Task UpdateConfigurationAsync(Guid id, UpdateTenantConfigurationDto input);

        /// <summary>
        /// 获取即将过期的租户
        /// </summary>
        Task<PagedResultDto<TenantDto>> GetExpiringSoonTenantsAsync(int days = 7);

        /// <summary>
        /// 获取续期统计信息
        /// </summary>
        Task<TenantRenewalStatisticsDto> GetRenewalStatisticsAsync();
    }

    /// <summary>
    /// 延期订阅DTO
    /// </summary>
    public class ExtendSubscriptionDto
    {
        /// <summary>
        /// 延期天数
        /// </summary>
        public int Days { get; set; }

        /// <summary>
        /// 延期原因
        /// </summary>
        public string? Reason { get; set; }

        /// <summary>
        /// 是否自动续期
        /// </summary>
        public bool AutoRenewal { get; set; } = false;

        /// <summary>
        /// 支付信息
        /// </summary>
        public Dictionary<string, object>? PaymentInfo { get; set; }
    }

    /// <summary>
    /// 设置试用DTO
    /// </summary>
    public class SetTrialDto
    {
        /// <summary>
        /// 试用开始时间
        /// </summary>
        public DateTime StartTime { get; set; }

        /// <summary>
        /// 试用结束时间
        /// </summary>
        public DateTime EndTime { get; set; }

        /// <summary>
        /// 最大用户数
        /// </summary>
        public int MaxUserCount { get; set; } = 10;

        /// <summary>
        /// 最大存储空间（MB）
        /// </summary>
        public long MaxStorageSize { get; set; } = 512;

        /// <summary>
        /// 功能限制
        /// </summary>
        public List<string> FeatureLimitations { get; set; } = new();
    }

    /// <summary>
    /// 设置管理员信息DTO
    /// </summary>
    public class SetAdminInfoDto
    {
        /// <summary>
        /// 管理员邮箱
        /// </summary>
        public string AdminEmail { get; set; } = string.Empty;

        /// <summary>
        /// 管理员手机号
        /// </summary>
        public string? AdminPhoneNumber { get; set; }

        /// <summary>
        /// 是否发送欢迎邮件
        /// </summary>
        public bool SendWelcomeEmail { get; set; } = true;

        /// <summary>
        /// 临时密码
        /// </summary>
        public string? TemporaryPassword { get; set; }
    }

    /// <summary>
    /// 设置连接信息DTO
    /// </summary>
    public class SetConnectionInfoDto
    {
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        public string? ConnectionString { get; set; }

        /// <summary>
        /// 主机地址
        /// </summary>
        public string? Host { get; set; }

        /// <summary>
        /// 是否立即测试连接
        /// </summary>
        public bool TestConnection { get; set; } = true;

        /// <summary>
        /// 连接超时（秒）
        /// </summary>
        public int ConnectionTimeout { get; set; } = 30;
    }

    /// <summary>
    /// 更新租户设置DTO
    /// </summary>
    public class UpdateTenantSettingsDto
    {
        /// <summary>
        /// 租户设置（JSON格式）
        /// </summary>
        public Dictionary<string, object>? Settings { get; set; }

        /// <summary>
        /// 自定义属性（JSON格式）
        /// </summary>
        public Dictionary<string, object>? CustomProperties { get; set; }

        /// <summary>
        /// 最大用户数
        /// </summary>
        public int? MaxUserCount { get; set; }

        /// <summary>
        /// 最大存储空间（MB）
        /// </summary>
        public long? MaxStorageSize { get; set; }

        /// <summary>
        /// 通知设置
        /// </summary>
        public NotificationSettings? NotificationSettings { get; set; }
    }

    /// <summary>
    /// 更新功能开关DTO
    /// </summary>
    public class UpdateFeatureSwitchesDto
    {
        /// <summary>
        /// 功能开关（JSON格式）
        /// </summary>
        public Dictionary<string, bool>? FeatureSwitches { get; set; }
    }

    /// <summary>
    /// 更新主题设置DTO
    /// </summary>
    public class UpdateThemeSettingsDto
    {
        /// <summary>
        /// 主题设置（JSON格式）
        /// </summary>
        public Dictionary<string, object>? ThemeSettings { get; set; }

        /// <summary>
        /// Logo URL
        /// </summary>
        public string? LogoUrl { get; set; }

        /// <summary>
        /// Favicon URL
        /// </summary>
        public string? FaviconUrl { get; set; }

        /// <summary>
        /// 自定义CSS
        /// </summary>
        public string? CustomCss { get; set; }
    }

    /// <summary>
    /// 获取需要续期的租户DTO
    /// </summary>
    public class GetTenantsNeedingRenewalDto : PagedAndSortedResultRequestDto
    {
        /// <summary>
        /// 天数阈值（默认7天）
        /// </summary>
        public int DaysThreshold { get; set; } = 7;

        /// <summary>
        /// 租户类型
        /// </summary>
        public TenantType? Type { get; set; }
    }

    /// <summary>
    /// 获取过期租户DTO
    /// </summary>
    public class GetExpiredTenantsDto : PagedAndSortedResultRequestDto
    {
        /// <summary>
        /// 是否包含已删除的租户
        /// </summary>
        public bool IncludeDeleted { get; set; } = false;

        /// <summary>
        /// 过期开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 过期结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }
    }

    /// <summary>
    /// 获取试用租户DTO
    /// </summary>
    public class GetTrialTenantsDto : PagedAndSortedResultRequestDto
    {
        /// <summary>
        /// 试用开始时间
        /// </summary>
        public DateTime? StartTime { get; set; }

        /// <summary>
        /// 试用结束时间
        /// </summary>
        public DateTime? EndTime { get; set; }

        /// <summary>
        /// 是否即将到期
        /// </summary>
        public bool ExpiringSoon { get; set; } = false;
    }

    /// <summary>
    /// 获取超限租户DTO
    /// </summary>
    public class GetTenantsWithLimitExceededDto : PagedAndSortedResultRequestDto
    {
        /// <summary>
        /// 限制类型
        /// </summary>
        public LimitType? LimitType { get; set; }

        /// <summary>
        /// 超限百分比阈值
        /// </summary>
        public double? ExceededPercentageThreshold { get; set; }
    }

    /// <summary>
    /// 搜索租户DTO
    /// </summary>
    public class SearchTenantsDto : PagedAndSortedResultRequestDto
    {
        /// <summary>
        /// 关键词
        /// </summary>
        public string Keyword { get; set; } = string.Empty;

        /// <summary>
        /// 租户类型
        /// </summary>
        public TenantType? Type { get; set; }

        /// <summary>
        /// 租户状态
        /// </summary>
        public TenantStatus? Status { get; set; }

        /// <summary>
        /// 是否启用
        /// </summary>
        public bool? IsEnabled { get; set; }
    }

    /// <summary>
    /// 批量操作租户DTO
    /// </summary>
    public class BatchOperationTenantDto
    {
        /// <summary>
        /// 租户ID列表
        /// </summary>
        public List<Guid> TenantIds { get; set; } = new();

        /// <summary>
        /// 操作类型
        /// </summary>
        public BatchOperationType Operation { get; set; }

        /// <summary>
        /// 操作参数
        /// </summary>
        public Dictionary<string, object>? Parameters { get; set; }

        /// <summary>
        /// 操作原因
        /// </summary>
        public string? Reason { get; set; }
    }

    /// <summary>
    /// 导出租户DTO
    /// </summary>
    public class ExportTenantsDto
    {
        /// <summary>
        /// 导出格式
        /// </summary>
        public ExportFormat Format { get; set; } = ExportFormat.Excel;

        /// <summary>
        /// 租户类型
        /// </summary>
        public TenantType? Type { get; set; }

        /// <summary>
        /// 租户状态
        /// </summary>
        public TenantStatus? Status { get; set; }

        /// <summary>
        /// 是否包含配置信息
        /// </summary>
        public bool IncludeConfigurations { get; set; } = false;

        /// <summary>
        /// 是否包含使用统计
        /// </summary>
        public bool IncludeUsageStatistics { get; set; } = false;
    }

    /// <summary>
    /// 导入租户DTO
    /// </summary>
    public class ImportTenantsDto
    {
        /// <summary>
        /// 文件内容（Base64编码）
        /// </summary>
        public string FileContent { get; set; } = string.Empty;

        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { get; set; } = string.Empty;

        /// <summary>
        /// 文件格式
        /// </summary>
        public ImportFormat Format { get; set; } = ImportFormat.Excel;

        /// <summary>
        /// 是否覆盖已存在的租户
        /// </summary>
        public bool OverwriteExisting { get; set; } = false;

        /// <summary>
        /// 是否验证数据
        /// </summary>
        public bool ValidateData { get; set; } = true;
    }

    /// <summary>
    /// 重置管理员密码DTO
    /// </summary>
    public class ResetAdminPasswordDto
    {
        /// <summary>
        /// 新密码（如果为空则生成随机密码）
        /// </summary>
        public string? NewPassword { get; set; }

        /// <summary>
        /// 是否发送邮件通知
        /// </summary>
        public bool SendEmailNotification { get; set; } = true;

        /// <summary>
        /// 邮件模板
        /// </summary>
        public string? EmailTemplate { get; set; }

        /// <summary>
        /// 重置原因
        /// </summary>
        public string? Reason { get; set; }
    }

    /// <summary>
    /// 更新租户配置DTO
    /// </summary>
    public class UpdateTenantConfigurationDto
    {
        /// <summary>
        /// 配置键
        /// </summary>
        public string Key { get; set; } = string.Empty;

        /// <summary>
        /// 配置值
        /// </summary>
        public string Value { get; set; } = string.Empty;

        /// <summary>
        /// 配置组
        /// </summary>
        public string? Group { get; set; }

        /// <summary>
        /// 是否加密存储
        /// </summary>
        public bool IsEncrypted { get; set; } = false;

        /// <summary>
        /// 配置描述
        /// </summary>
        public string? Description { get; set; }
    }

    /// <summary>
    /// 通知设置
    /// </summary>
    public class NotificationSettings
    {
        /// <summary>
        /// 是否启用邮件通知
        /// </summary>
        public bool EnableEmailNotifications { get; set; } = true;

        /// <summary>
        /// 是否启用短信通知
        /// </summary>
        public bool EnableSmsNotifications { get; set; } = false;

        /// <summary>
        /// 是否启用系统内通知
        /// </summary>
        public bool EnableInternalNotifications { get; set; } = true;

        /// <summary>
        /// 过期提醒天数
        /// </summary>
        public int ExpirationReminderDays { get; set; } = 7;

        /// <summary>
        /// 使用量提醒阈值（百分比）
        /// </summary>
        public double UsageReminderThreshold { get; set; } = 80;
    }

    /// <summary>
    /// 批量操作类型
    /// </summary>
    public enum BatchOperationType
    {
        /// <summary>
        /// 激活
        /// </summary>
        Activate = 1,

        /// <summary>
        /// 暂停
        /// </summary>
        Suspend = 2,

        /// <summary>
        /// 禁用
        /// </summary>
        Disable = 3,

        /// <summary>
        /// 启用
        /// </summary>
        Enable = 4,

        /// <summary>
        /// 续期
        /// </summary>
        Extend = 5,

        /// <summary>
        /// 删除
        /// </summary>
        Delete = 6
    }

    /// <summary>
    /// 导出格式
    /// </summary>
    public enum ExportFormat
    {
        /// <summary>
        /// Excel
        /// </summary>
        Excel = 1,

        /// <summary>
        /// CSV
        /// </summary>
        Csv = 2,

        /// <summary>
        /// JSON
        /// </summary>
        Json = 3,

        /// <summary>
        /// XML
        /// </summary>
        Xml = 4
    }

    /// <summary>
    /// 导入格式
    /// </summary>
    public enum ImportFormat
    {
        /// <summary>
        /// Excel
        /// </summary>
        Excel = 1,

        /// <summary>
        /// CSV
        /// </summary>
        Csv = 2,

        /// <summary>
        /// JSON
        /// </summary>
        Json = 3,

        /// <summary>
        /// XML
        /// </summary>
        Xml = 4
    }

    /// <summary>
    /// 租户续期统计DTO
    /// </summary>
    public class TenantRenewalStatisticsDto
    {
        /// <summary>
        /// 总租户数
        /// </summary>
        public int TotalTenants { get; set; }

        /// <summary>
        /// 启用自动续期的租户数
        /// </summary>
        public int AutoRenewalEnabledCount { get; set; }

        /// <summary>
        /// 禁用自动续期的租户数
        /// </summary>
        public int AutoRenewalDisabledCount { get; set; }

        /// <summary>
        /// 本月续期的租户数
        /// </summary>
        public int MonthlyRenewals { get; set; }

        /// <summary>
        /// 本年续期的租户数
        /// </summary>
        public int YearlyRenewals { get; set; }

        /// <summary>
        /// 平均续期天数
        /// </summary>
        public double AverageRenewalDays { get; set; }

        /// <summary>
        /// 续期成功率（百分比）
        /// </summary>
        public double RenewalSuccessRate { get; set; }

        /// <summary>
        /// 续期次数分布
        /// </summary>
        public Dictionary<int, int> RenewalCountDistribution { get; set; } = new();

        /// <summary>
        /// 下月即将过期的租户数
        /// </summary>
        public int ExpiringNextMonth { get; set; }

        /// <summary>
        /// 统计时间
        /// </summary>
        public DateTime StatisticsTime { get; set; } = DateTime.UtcNow;
    }
}